<!--
 * @Description: 商品列表模块
 * @Author: pinus
 * @Email: pinus0716@163.com
 * @Git: https://gitee.com/xyf97
 * @Date: 2021-10-01 19:27:32
 * @LastEditTime: 2021-10-02 15:32:50
-->
<template>
  <div class="list-container">
    <!--  头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/wlecome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 内容card -->
    <el-card>
      <!-- 搜索栏与添加栏 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容"
            clearable
            v-model="getParams.query"
            @clear="clearSearch">
            <el-button slot="append"
              icon="el-icon-search"
              @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary"
            @click="addGood">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品表格 -->
      <el-table :data="goodsList"
        border>
        <el-table-column type="index"
          label="#"
          align="center"> </el-table-column>
        <el-table-column prop="goods_name"
          width="400"
          label="商品名"
          align="center"> </el-table-column>
        <el-table-column prop="goods_price"
          label="价格"
          align="center"> </el-table-column>
        <el-table-column prop="goods_number"
          label="数量"
          align="center"> </el-table-column>
        <el-table-column prop="goods_weight"
          label="重量"
          align="center"> </el-table-column>
        <el-table-column align="center"
          label="更新时间">
          <template slot-scope="scope">
            <!-- 调用过滤器规范时间格式 -->
            {{ scope.row.upd_time | dateFormate }}
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作"
          width="210"
          align="center">
          <template slot-scope="scope">
            <!-- 修改信息按钮 -->
            <el-button size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="editGood(scope.row)">编辑</el-button>
            <!-- 删除用户按钮 -->
            <el-button size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeGood(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 换页区域 -->
      <el-pagination layout="total, sizes, prev, pager, next, jumper"
        :current-page="getParams.pagenum"
        :page-sizes="[10, 20, 30]"
        :page-size="getParams.pagesize"
        :total="totalGoods"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"> </el-pagination>
    </el-card>

    <!-- 修改商品信息弹窗 -->
    <el-dialog title="修改用户信息"
      :visible.sync="editdialogVisible">
      <!-- 商品信息表单 -->
      <el-form ref="editGoodFormRef"
        :model="editGoodForm"
        label-width="auto"
        :rules="editGoodFormRules"
        status-icon>
        <el-form-item label="商品名称"
          prop="goods_name">
          <el-input v-model="editGoodForm.goods_name"
            :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="价格（元）"
          prop="goods_price">
          <el-input v-model="editGoodForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="数量"
          prop="goods_number">
          <el-input v-model="editGoodForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="重量（公斤）"
          prop="goods_weight">
          <el-input v-model="editGoodForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部操控区域 -->
      <div slot="footer">
        <el-button @click="closeEditGood">取 消</el-button>
        <el-button type="primary"
          @click="submitEdit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'List',
  created() {
    this.getGoodsList()
  },
  data() {
    return {
      // get参数
      getParams: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      totalGoods: 0,
      // 商品列表
      goodsList: [],
      // 编辑对话框
      goodId: 0,
      editdialogVisible: false,
      editGoodForm: {
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0
      },
      editGoodFormRules: {
        goods_name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 6, message: '价格在 1 到 10^6 元', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 4, message: '数量在 1 到 10^4 个', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 5, message: '重量在 1 到 5 公斤', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 添加商品
    addGood() {
      this.$router.push('/add')
    },
    // 请求用户数据列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.getParams
      })

      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.goodsList = res.data.goods
      this.getParams.pagenum = parseInt(res.data.pagenum)
      this.totalGoods = res.data.total
    },
    // 改变一页展示数目
    handleSizeChange(val) {
      this.getParams.pagesize = val
      this.getGoodsList()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.getParams.pagenum = val
      this.getGoodsList()
    },
    // 修改用户信息
    editGood(row) {
      this.editGoodForm.goods_name = row.goods_name
      this.editGoodForm.goods_price = row.goods_price
      this.editGoodForm.goods_number = row.goods_number
      this.editGoodForm.goods_weight = row.goods_weight
      this.goodId = row.goods_id
      this.editdialogVisible = true
    },
    // 删除用户
    async removeGood(row) {
      const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (confirm !== 'confirm') {
        return this.$message.info('取消删除')
      }

      const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      this.getGoodsList()
      this.$message.success(res.meta.msg)
    },
    // 清空搜索结果
    clearSearch() {
      this.query = ''
      this.getGoodsList()
    },
    // 关闭编辑对话框
    closeEditGood() {
      this.editdialogVisible = false
      // 重置表单
      this.$refs.editGoodFormRef.resetFields()
    },
    // 提交标记表单
    async submitEdit() {
      const { data: res } = await this.$http.put(`goods/${this.goodId}`, this.editGoodForm)
      if (res.meta.status !== 201) {
        return this.$message.error(res.meta.msg)
      }

      this.getGoodsList()
      this.$message.success(res.meta.msg)
      this.editdialogVisible = false
    }
  }
}
</script>

<style>
.el-table {
  margin-top: 10px;
}
</style>
